package cn.pconline.officialclub.web;

import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.util.DateUtils;
import cn.pconline.autoclub.util.Result;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.officialclub.entity.OClubPv;
import cn.pconline.officialclub.service.OClubService;
import cn.pconline.officialclub.service.OStatService;
import com.alibaba.fastjson.JSONArray;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 人气统计等相关
 *
 * @author <a href="mailto:chensy0203@163.com">chensy</a>
 */
@Controller
@RequestMapping("/brand/stat")
public class OStatController extends WebKit {

    @Autowired
    private OStatService oStatService;
    @Autowired
    private OClubService oClubService;

    @RequestMapping(value = "/getPv.do", method = RequestMethod.GET)
    @ResponseBody
    public void getPv(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        WebUtils.setEncoding(req, resp);
        long oClubId = WebUtils.paramLong(req, "oClubId", 0);
        long total = oStatService.countPvByClubId(oClubId);
        long today = oStatService.countPvByClubId(oClubId, DateUtils.getTodayYmd());
        printMsg(resp, Result.instance().set("total", total).set("today", today));
    }

    @RequestMapping(value = "/getLatestPv.do", method = RequestMethod.GET)
    @ResponseBody
    public void getLatestPv(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        WebUtils.setEncoding(req, resp);
        String param = WebUtils.param(req, "param", "");
        String[] params = param.split(",");
        for (String p : params) {
            String[] t = p.split(":");
            if (t.length != 2) {
                continue;
            }
            String key = t[0];
            String value = t[1];
            req.setAttribute(key, value);
        }
        req.setCharacterEncoding("GBK");
        resp.setContentType("text/html; charset=GBK");
        long oClubId = WebUtils.paramLong(req, "oClubId", 0);
        List<OClubPv> list = oStatService.getLatestClubPv(oClubId, 7);
        JSONArray arr = oStatService.toJSONArray(list);
        printMsg(resp, Result.instance().set("history", arr));
    }

    @RequestMapping(value = "/addVisitor.do", method = RequestMethod.POST)
    @ResponseBody
    public void addVisitor(HttpServletRequest req, HttpServletResponse resp) {
        WebUtils.setEncoding(req, resp);
        long oClubId = WebUtils.paramLong(req, "oClubId", 0);
        if (oClubId > 0 && oClubService.findValidClub(oClubId) != null) {
            User user = checkUser(req, resp);
            if (user == null) {
                return;
            }
            int count = oStatService.addOrUpdateVisitor(user.getUid(), oClubId);
            if (count > 0) {
                printMsg(resp, Result.dataError("操作成功"));
            } else {
                printMsg(resp, Result.error("操作失败"));
            }
        } else {
            printMsg(resp, Result.dataError("clubId参数有误"));
        }
    }

    @RequestMapping(value = "/addPv.do", method = RequestMethod.POST)
    @ResponseBody
    public void addPv(HttpServletRequest req, HttpServletResponse resp) {
        WebUtils.setEncoding(req, resp);
        long oClubId = WebUtils.paramLong(req, "oClubId", 0);
        if (oClubId > 0) {
            oStatService.createOrUpdatePv(oClubId, DateUtils.getTodayYmd());
            printMsg(resp, Result.success());
        } else {
            printMsg(resp, Result.dataError());
        }
    }

    @RequestMapping(value = "/getTotalPv.do", method = RequestMethod.GET)
    @ResponseBody
    public void getTotalPv(HttpServletRequest req, HttpServletResponse resp) {
        WebUtils.setEncoding(req, resp);
        long clubId = WebUtils.paramLong(req, "oClubId", 0);
        long total = oStatService.countPvByClubId(clubId);
        printMsg(resp, Result.instance().set("total", total));
    }

    @RequestMapping(value = "/getMemberNum.do", method = RequestMethod.GET)
    @ResponseBody
    public void getMemberNum(HttpServletRequest req, HttpServletResponse resp) {
        WebUtils.setEncoding(req, resp);
        long oClubId = WebUtils.paramLong(req, "oClubId", 0);
        int memberNum = oMemberService.countCommonMember(oClubId);
        printMsg(resp, Result.instance().set("memberNum", memberNum));
    }
}
